<?php
/**
 *
 * Projet Authentic CMS
 * @file 	   system.php
 *
 * PHP version 5
 *
 *
 * @author     Poly Charly <CharlyPoly@gmail.fr>
 * @copyright  2009 Authentic Development Group
 * @license    GNU General Public License v3
 * @since      File available since Version 0.1.0
 * 
 */

class System {
	private static $_loaded	= array();
	
	public static function load($path) { // System::load('core.utils.object');
		// Single load
		if (isset(self::$_loaded[$path])) return false;
		self::$_loaded[$path]	= true;
		
		//Load Now..
		if (strpos($path, '.') != -1) {
			$dirs = explode(".", $path);
			$file = array_pop($dirs);
			return is_file(SYSTEM_DIR.implode(DS, $dirs).DS.$file.'.php') ? include(SYSTEM_DIR.implode(DS, $dirs).DS.$file.'.php') : FALSE;
			
		} else {
			return is_file(SYSTEM_DIR.$path.'.php') ? include(SYSTEM_DIR.$path.'php') : FALSE;
		}
	}

	public function start() {
		self::load('config.Config');
		$config = new config();		
		
		self::load('core.router');
		self::load('core.request');
		try {
			$request = new request();
			$router = new router($config->Site['Index'], $config->System['base']);
			
			if (!$router->isAdmin() && $config->Site['Status'] == "Closed") {
				self::load('error.exception.siteclosed');
				throw new SiteClosed($config->Site['CloseMess']);
			}
			
			$this->dispatch($request, $router);
		} catch (Exception $e) {
		}
	}
	
	private function dispatch($request, $router) {
		//  #ROUTES 
			//Default
			$module			= $controller = $router->getController();
			$action			= $router->getAction();
			$args			= $router->getArgs();
			
			// System Custom routes.
			$SystemCustomRoutes = parse_ini_file(SYSTEM_DIR.'config'.DS.'routes.php', true);
			if (array_key_exists($router->getController(), $SystemCustomRoutes['routes'])) {
				$a 					= explode('.', $SystemCustomRoutes['routes'][$router->getController()]);
				$ParamsLength		= count($a);
				$args				= array();
				foreach( $a as $k=>$v ) {
					if ($ParamsLength > 2 && $k > 2) {
						$args[] = $v;
					}
				}
				$module				= $a[0];
				$controller 		= $a[1];
				$action 			= (isset($a[2]) && !empty($a[2])) ? $a[2] : 'index';
			}
			
			//  Module Custom routes.
			if (is_dir(MODULES_DIR.$module) && is_file(MODULES_DIR.$module.DS.'routes.php')) { 
			
					$ModRoutes = parse_ini_file(MODULES_DIR.$module.DS.'routes.php', true);
					
					$path	   = $controller.'/'.$action.'/'.implode('/', $args);
					
					if (array_key_exists($path, $ModRoutes['routes'])) {
						$nwPath = str_replace($path, '' , $ModRoutes['routes'][$path]);
						$controller = $ModRoutes['routes'][$path];
						$a 					= explode('/', $nwPath);
						$ParamsLength		= count($a);
						$args				= array();
						foreach( $a as $k=>$v ) {
							if ($ParamsLength > 1 && $k > 1) {
								$args[] = $v;
							}
						}
						$action 		= (isset($a[1]) && !empty($a[1])) ? $a[1] : 'index';
					} 
			}
			
			
//			echo $controller.' - '.$action.' - '.implode(' | ', $args);
			
			try {
				$this->run($request, $module, $controller, $action, $args);
			} catch (ModuleException $e) {
				print($e);
				die();
			} catch (ActionException $e) {
				print($e);
				die();
			} catch (SystemException $e) {
				print($e);
				die();
			} catch (Exception $e) {
				print($e);
				die();
			}
			
			
			
	}
	
		
	static public function run($request, $module, $controller, $action, $args) {
		self::load('controller');
		self::load('records');
		self::load('wiew');
		//  #Init Wiew
		$wiew = new wiew('default', md5($module.$controller.$action.implode('-', $args)));
		
		//$wiew->Startob();
		
		if (is_dir(MODULES_DIR.$module)) {
				
				
				include(MODULES_DIR.$module.DS.$controller.'_controller.php');
				// Start Module
				$class		= $controller.'_controller';
				$thread		= new $class;
				//Init Controller
				$thread->init($request, $module);
				
				// Do Action
				if (method_exists($thread, $action)) {
					$thread->{$action}($args);
				} else {
					self::load('error.exception.action');
					throw new ActionException('Undefined Action ['.$action.']');
				}
				
				
				
		} else {
			self::load('error.exception.module');
			throw new ModuleException('Can\'t find Module ! ['.$controller.']');
		}
		
		//$wiew->Stopob();
		
		#  Wiew Act
		$wiew->setContent($thread->getWiew());
		
		$wiew->display();
		
		
	}
}


?>